package cn.qing.service.Impl;

import cn.qing.mapper.UserMapper;
import cn.qing.pojo.Role;
import cn.qing.pojo.UserInfo;
import cn.qing.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    //注入加密的对象
    @Autowired
    BCryptPasswordEncoder encoder;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        UserInfo userInfo = userMapper.findUserByUsername(username);

        //获取用户的角色信息  不写死
        List<Role> roles = userInfo.getRoles();

        //创建User对象，把userInfo中的内容封到User中
        //明文需要用{noop} 密文不需要
        User user = new User(userInfo.getUsername(),userInfo.getPassword(),
                userInfo.getStatus() !=0,true,true,true,getAuthority(roles));
        return user;
    }

    /**
     * 获取角色列表
     * @return authorities 角色列表
     */
    public List<SimpleGrantedAuthority> getAuthority(List<Role> roles){

        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        /*authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
        authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));*/
        // 从数据库中查询角色
        for (Role role : roles){
            authorities.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));
        }

        return authorities;
    }

    /**
     * 查找user用户
     * @param page
     * @param size
     * @return
     */
    @Override
    public List<UserInfo> findAll(Integer page, Integer size) {

        PageHelper.startPage(page,size);
        List<UserInfo> userInfoList = userMapper.findAll();
        return userInfoList;
    }

    /**
     * 保存用户
     * @param userInfo
     */
    @Override
    public void save(UserInfo userInfo) {
        //加密
        String encode = encoder.encode(userInfo.getPassword());
        userInfo.setPassword(encode);
        userMapper.save(userInfo);
    }

    /**
     * 用户详情
     * @param id
     * @return
     */
    @Override
    public UserInfo findById(Integer id) {

        UserInfo userInfo = userMapper.findById(id);
        return userInfo;
    }

    /**
     * 根据userid查询用户可以添加的角色（就是没有的角色）
     * @param userId
     * @return
     */
    @Override
    public List<Role> findOtherRoles(Integer userId) {

        List<Role> roleList = userMapper.findOtherRoles(userId);
        return roleList;
    }

    /**
     * 保存 给用户添加角色
     * @param userId
     * @param ids
     * @return
     */
    @Override
    public void addRoleToUser(Integer userId,Integer[] ids) {
        for (Integer roleId : ids){
            userMapper.addRoleToUser(userId,roleId);
        }
    }
}
